{*
  Object Pascal simple currency manipulation routines
  @Author  Maciej Zagozda (1998-2008)
  @Licence Microsoft Reciprocal License (Ms-RL)
  @Website http://www.codeplex.com/intelitechdelphirtns/
  @Version 2008/04/20 Added documentation support
}

unit CurrencyRoutines;

interface

uses
  Classes,
  Controls,
  Dialogs,
  ExtCtrls,
  Forms,
  Graphics,
  Messages,
  StdCtrls,
  SysUtils,
  Windows;

type
  EUnicodeToCurrencyDecimalError = class(Exception);

function CurrencyToString( currValue: Currency; strnSymbol: AnsiString ): AnsiString;
function CurrencyToUnicode( currValue: Currency ): AnsiString;
function FormatPercent( doblValue: Double ): AnsiString;
function UnicodeToCurrency( strnValue: AnsiString ): Currency;

implementation

function CurrencyToString( currValue: Currency; strnSymbol: AnsiString ): AnsiString;
begin
  if Length( strnSymbol ) = 0 then strnSymbol := 'PLN';
  Result := Trim( FloatToStrF( currValue, ffNumber, 18, 2 ) + ' ' + strnSymbol );
end;

//_____________________________________________________________________________

function CurrencyToUnicode( currValue: Currency ): AnsiString;
begin
  Result := FormatFloat( '0.00', currValue );
end;

//_____________________________________________________________________________

function FormatPercent( doblValue: Double ): AnsiString;
begin
  Result := FloatToStrF( doblValue * 100, ffNumber, 8, 2 ) + ' %';
end;

//_____________________________________________________________________________

function StringToCurrency( strnValue: AnsiString ): Currency;
begin
  Result := 0;
end;

//_____________________________________________________________________________

function UnicodeToCurrency( strnValue: AnsiString ): Currency;
begin
  //ShowMessage( StringReplace( strnValue, ',', '.', [rfReplaceAll, rfIgnoreCase] ));
  if DecimalSeparator = '.' then
  begin
    Result := StrToCurr( StringReplace( strnValue, ',', '.', [rfReplaceAll, rfIgnoreCase] ));
  end
  else
  if DecimalSeparator = ',' then
  begin
    Result := StrToCurr( StringReplace( strnValue, '.', ',', [rfReplaceAll, rfIgnoreCase] ));
  end
  else
  begin
    raise EUnicodeToCurrencyDecimalError.CreateFMT( 'Invalid decimal symbol', [DecimalSeparator] );
    Result := 0; 
  end;//if   

end;

end.
